﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using vJine.Core;
using vJine.Lua.GUI.UI;

namespace vJine.Lua.GUI {
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class WinDebug : Window {
        public WinDebug() {
            InitializeComponent();

            this.MinWidth = this.Width;
            this.MinHeight = this.Height;
        }

        LuaContext luaContext;
        NodeConfig luaConfig;

        public WinDebug set_bound(LuaContext luaContext, NodeConfig luaConfig) {

            this.luaContext = luaContext;
            this.luaConfig = luaConfig;

            this.trvConfigs.Items.Clear();
            this.trvConfigs.ItemsSource = this.luaConfig;

            return this;
        }
        
        private void trvConfigs_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) {
            NodeConfig luaConfig = e.NewValue as NodeConfig;
            this.nodeStatus.set_bound(null, this.luaContext, luaConfig);

            bool is_running = false;
            luaConfig.on_begin += (NodeConfig cmd) => {
                lock (this) {
                    is_running = true;
                    this.trvConfigs.IsEnabled = false;
                }
            };
            luaConfig.on_finish += (NodeConfig cmd) => {
                lock (this) {
                    is_running = false;
                    this.trvConfigs.IsEnabled = true;
                }
            };
            this.Closing += (object cSender, CancelEventArgs cE) => {
                lock (this) {
                    cE.Cancel = is_running;
                }
            };
        }
    }    
}
